<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkMessage\Command;

use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use MoChat\App\WorkMessage\Fetcher\MessageDataFetcher;
use MoChat\App\WorkMessage\Queue\MessagePullQueue;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
 * @Command
 */
class MessageSyncTask extends HyperfCommand
{
    /**
     * @Inject
     */
    protected ContainerInterface $container;

    private StdoutLoggerInterface $logger;

    /**
     * @Inject
     */
    private MessageDataFetcher $messageDataFetcher;

    /**
     * @Inject
     */
    private MessagePullQueue $messagePullQueue;

    public function __construct()
    {
        parent::__construct('mochat:message-sync');
    }

    public function handle()
    {
        $this->logger = $this->container->get(StdoutLoggerInterface::class);
        $corpId = $this->input->getArgument('id');

        if (empty($corpId)) {
            $this->logger->debug('没有满足条件同步消息的企业');
            return;
        }
        while (true) {
            $this->logger->debug(sprintf('加入拉取会话消息队列，企业ID：%s', (string) $corpId));
            $this->messagePullQueue->handle((int) $corpId);
            sleep(1);
        }
    }

    protected function configure()
    {
        $this->setDescription('同步消息!');
        $this->addArgument('id', InputArgument::OPTIONAL, 'id', 0);
    }

    protected function getArguments(): array
    {
        return [
            ['id', InputArgument::OPTIONAL, '这里是对这个参数的解释'],
        ];
    }
}
